package drone.facade.model.entity ;


import java.lang.reflect.Field ;
import java.util.Objects ;

import drone.basis.base.BaseBean ;
import drone.basis.meta.FieldMeta ;

public class BaseEntity extends BaseBean
{

	private static final long serialVersionUID = 1L ;

	public boolean validate ( )
	{
		for ( Field field : this.getClass ( ).getDeclaredFields ( ) )
		{
			field.setAccessible ( true ) ;
			if ( EXCLUDE.contains ( field.getName ( ) ) )
			{
				continue ;
			}
			if ( field.getAnnotation ( FieldMeta.class ).require ( ) )
			{
				try
				{
					if ( Objects.isNull ( field.get ( this ) ) )
					{
						return false ;
					}
				} catch ( Exception e )
				{
					return false ;
				}
			}
		}
		return true ;
	}

	public String primaryValue ( )
	{
		for ( Field field : this.getClass ( ).getDeclaredFields ( ) )
		{
			field.setAccessible ( true ) ;
			if ( EXCLUDE.contains ( field.getName ( ) ) )
			{
				continue ;
			}
			if ( field.getAnnotation ( FieldMeta.class ).primary ( ) )
			{
				try
				{
					if ( !Objects.isNull ( field.get ( this ) ) )
					{
						return field.get ( this ).toString ( ) ;
					}
				} catch ( Exception e )
				{
					return null ;
				}
			}
		}
		return null ;
	}

}
